package com.bawei.bydayzg_two.viewModel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.bawei.base.bean.CouponsResult
import com.bawei.base.bean.GoodsResult
import com.bawei.base.bean.IndexResult
import com.bawei.base.bean.Res
import com.bawei.base.http.SingleLiveData
import com.bawei.base.mvvm.BaseViewModel
import kotlinx.coroutines.launch

class HomeViewModel:BaseViewModel() {

    val indexSuccess = SingleLiveData<Res<IndexResult>>()
    val indexFail = SingleLiveData<String>()
    //
    fun index(){
        viewModelScope.launch {
            try {
                indexSuccess.postValue(apiService.Index())
            } catch (e:Exception){
                indexFail.postValue(e.message.toString())
            }
        }
    }

    //
    val couponsSuccess = SingleLiveData<Res<CouponsResult>>()
    val couponsFail = SingleLiveData<String>()
    //
    fun coupons(limit:String,page:String,type:String){
        viewModelScope.launch {
            try {
                couponsSuccess.postValue(apiService.coupons(limit, page, type))
            } catch (e:Exception){
                couponsFail.postValue(e.message.toString())
            }
        }
    }

    //首页商品列表
    val indexGoodsSuccess = SingleLiveData<Res<GoodsResult>>()
    val indexGoodsFail = SingleLiveData<String>()
    //
    fun indexGoods(type:Int,limit:Int,page:Int){
        viewModelScope.launch {
            try {
                indexGoodsSuccess.postValue(apiService.indexGoods(type, limit, page))
            } catch (e: Exception) {
                indexGoodsFail.postValue(e.message.toString())
            }

        }
    }

    //领取优惠卷
    val couponsGetSuccess = SingleLiveData<Res<String?>>()
    val couponsGetFail = SingleLiveData<String>()
    //
    fun couponsGet(couponId:Int){
        viewModelScope.launch {
            try {
                var map = mutableMapOf<String,Any>()
                map["couponId"] = couponId
                couponsGetSuccess.postValue(apiService.couponsGet(maoToBody(map)))
            } catch (e: Exception) {
                couponsGetFail.postValue(e.message.toString())
            }
        }
    }
}